library(tidyverse)
library(marginaleffects)
library(modelsummary)
library(sandwich)
library(ggplot2)
library(kableExtra)

load("extension.RData")

df <- df %>% mutate(pm = as.numeric(pm),
                    partner = as.numeric(partner & !pm))

### Figure 0 ----------------------------------------

ggplot(df, aes(x = country, y = net_concreteness,
               fill = country)) + 
  geom_violin(trim = TRUE) +
  geom_dotplot(binaxis = 'y', 
               stackdir = 'center', 
               dotsize = .25,
               binwidth = .04) +
  xlab("") +
  scale_y_continuous(name = "Manifesto Concreteness") +
  theme_gray() +
  theme(legend.position = "none")

#### Models

models <- 
  list(
    lm(net_concreteness ~ government * lag_gdp + as.factor(country),
       data = df),
    lm(net_concreteness ~ pm * lag_gdp + partner * lag_gdp + as.factor(country),
       data = df),
    lm(net_concreteness ~ government * lag_gdp,
       subset = prop_dummy == 1, data = df),
    lm(net_concreteness ~ government * lag_gdp,
       subset = prop_dummy == 0, data = df)
  )

### Figure 1 ----------------------------------------

plot_cap(models[[1]], 
         condition = c("lag_gdp", "government"), 
         conf_level = .95,
         vcov = sandwich(models[[1]], type = "robust")) +
  scale_x_continuous(name = "Economic Performance (GDP Growth Rate)",
                     breaks = seq(-5, 10, 5)) +
  scale_y_continuous(name = "Predicted Manifesto Concreteness",
                     breaks = seq(1.5, 3.5, .5),
                     limits = c(1.5, 3.5)
  ) +
  scale_fill_discrete(labels = c("Opposition", "Government")) +
  scale_color_discrete(labels = c("Opposition", "Government")) +
  theme_gray() +
  theme(legend.title = element_blank(),
        legend.position = c(.7, .85))

### Table 1 ----------------------------------------

modelsummary(models,
             stars = c('+' = .1, '*' = .05, '**' = .01),
             vcov = list(
               sandwich(models[[1]], type = "robust"),
               sandwich(models[[2]], type = "robust"),
               sandwich(models[[3]], type = "robust"),
               sandwich(models[[4]], type = "robust")),
             coef_map = c(
               "government" = "Incumbent party",
               "pmTRUE" = "Prime minister",
               "partnerTRUE" = "Government partner",
               "government:lag_gdp" = "Incumbent × performance",
               "pmTRUE:lag_gdp" = "Prime minister × performance",
               "lag_gdp:partnerTRUE" = "Partner × performance",
               "lag_gdp" = "Economic performance",
               "as.factor(country)France" = "France",
               "as.factor(country)Germany" = "Germany",
               "as.factor(country)Ireland" = "Ireland",
               "as.factor(country)Italy" = "Italy",
               "as.factor(country)Netherlands" = "Netherlands",
               "as.factor(country)Norway" = "Norway",
               "as.factor(country)Portugal" = "Portugal",
               "as.factor(country)Spain" = "Spain",
               "as.factor(country)United Kingdom" = "United Kingdom",
               "(Intercept)" = "Constant"),
             gof_map = tribble(
               ~raw, ~clean, ~fmt,
               "nobs", "No. of Observations", 0,
               "logLik", "Log likelihood", 3),
             title = "Political Factors That Motivate Clear Party Reputation Signals",
             escape = F#, output = "latex"
             ) %>%
  kable_styling(latex_options = c("hold_position"),
                font_size = 11) %>%
  footnote(general = "Standard errors in parentheses.",
           footnote_as_chunk = T)

### Figure 2 ----------------------------------------

mod_inc <- lm(net_concreteness ~ new_inc * lag_gdp
              + as.factor(country),
              data = df)

plot_cme(
  mod_inc,
  effect = "lag_gdp",
  condition = "new_inc",
  conf_level = .95,
  vcov = sandwich(mod_inc, type = "robust")) +
  geom_hline(yintercept = 0,
             linetype = "dotted") +
  labs(x = "Government Status") +
  scale_y_continuous(name = "Marginal Effect",
                     limits = c(-.1, .2)) +
  scale_color_discrete(labels = c('Low GDP Growth', 'High GDP Growth')) +
  scale_fill_discrete(labels = c('Low GDP Growth', 'High GDP Growth')) +
  theme_gray() 

#### NLP section

nlp <- read_csv("merged_nlp.csv")

### Figure 3 ----------------------------------------

ggplot(data = nlp, 
       aes(x = stdConW1, y = score)) +
  geom_point(aes(color = country,
                 shape = country)) +
  geom_smooth(method = "lm",
              color = "black") +
  geom_abline() +
  scale_x_continuous(name = "Dictionary scores") +
  scale_y_continuous(name = "NLP scores",
                     limits = 0:1) +
  theme_bw() +
  theme(legend.title = element_blank(),
        panel.grid = element_blank())

### Models -----------------------------------------------

models <- list(
  lm(score ~ periodincumb * gdpgrowthcpdsT1
     + partypolar + lnAge + niche + priorRVS
     + ctry2 + ctry3 + ctry4 + ctry5 + ctry6,
     data = nlp),
  lm(score ~ period_govduration_cabinet * gdpgrowthcpdsT1
     + partypolar + lnAge + niche + priorRVS
     + ctry2 + ctry3 + ctry4 + ctry5 + ctry6,
     data = nlp),
  lm(score ~ period_pmincumb * gdpgrowthcpdsT1 + period_onlycabincumb * gdpgrowthcpdsT1
     + partypolar + lnAge + niche + priorRVS
     + ctry2 + ctry3 + ctry4 + ctry5 + ctry6,
     data = nlp)
)

### Table 2: --------------------------------------------

modelsummary(models,
             stars = c('+' = .1, '*' = .05, '**' = .01),
             coef_map = c(
               "periodincumbIncumbent Party" = "Incumbent party",
               "period_govduration_cabinet" = "Governing duration (months)",
               "period_pmincumb" = "Prime minister or president",
               "period_onlycabincumb" = "Government partner",
               "periodincumbIncumbent Party:gdpgrowthcpdsT1" = "Incumbent × performance",
               "period_govduration_cabinet:gdpgrowthcpdsT1" = "Duration × performance",
               "period_pmincumb:gdpgrowthcpdsT1" = "Prime minister × performance",
               "gdpgrowthcpdsT1:period_onlycabincumb" = "Partner × performance",
               "gdpgrowthcpdsT1" = "Economic performance",
               "partypolar" = "Party polarization",
               "priorRVS" = "Previous electoral results",
               "lnAge" = "Party age",
               "niche" = "Niche party",
               "ctry2" = "Canada",
               "ctry3" = "Great Britain",
               "ctry4" = "Ireland",
               "ctry5" = "New Zealand",
               "ctry6" = "United States",
               "(Intercept)" = "Constant"),
             gof_map = tribble(
               ~raw, ~clean, ~fmt,
               "nobs", "No. of Observations", 0,
               "logLik", "Log likelihood", 3),
             title = "Political Factors That Motivate Clear Party Reputation Signals Estimated from a Semi-Supervised NLP Method \\label{tab:nlp_reg}",
             escape = F, #output = "latex"
             ) %>%
  kable_styling(latex_options = c("hold_position"),
                font_size = 11) %>%
  footnote(general = "Standard errors in parentheses.",
           footnote_as_chunk = T)

### Figure 4 ----------------------------------------

plot_cap(models[[1]], 
         condition = c("gdpgrowthcpdsT1", "periodincumb"), 
         conf_level = .9) +
  scale_x_continuous(name = "Economic Performance (GDP Growth Rate)",
                     breaks = seq(-2, 10, 2)) +
  scale_y_continuous(name = "Predicted Manifesto Concreteness",
                     breaks = seq(.1, .5, .05)) +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        axis.title = element_text(size=10)) 

### Figure 5 -----------------------------------------

plot_cap(models[[2]], 
         condition = list("period_govduration_cabinet",
                          "gdpgrowthcpdsT1" = c(-.07, 6.49)),
         conf_level = .9) +
  scale_x_continuous(name = "Government Duration (months)",
                     breaks = seq(0, 60, 20)) +
  scale_y_continuous(name = "Predicted Manifesto Concreteness",
                     breaks = seq(.2, .6, .05)) +
  scale_color_discrete(labels=c('Low GDP Growth', 'High GDP Growth')) +
  scale_fill_discrete(labels=c('Low GDP Growth', 'High GDP Growth')) +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        axis.title = element_text(size=10)) 


### Figure 6 -------------------------------------------

mod_inc <- lm(score ~ newInc * gdpgrowthcpdsT1 
              + partypolar + lnAge + priorRVS + niche
              + ctry2 + ctry3 + ctry4 + ctry5 + ctry6,
              data = nlp)

plot_cme(
  mod_inc,
  effect = "gdpgrowthcpdsT1",
  condition = "newInc",
  conf_level = .9) +
  geom_hline(yintercept = 0,
             linetype = "dotted") +
  labs(x = "Government Status") +
  scale_y_continuous(name = "Marginal Effect",
                     breaks = seq(-.06, .06, .02),
                     limits = c(-.06, .06)) +
  scale_color_discrete(labels = c('Low GDP Growth', 'High GDP Growth')) +
  scale_fill_discrete(labels = c('Low GDP Growth', 'High GDP Growth')) +
  theme_bw() +
  theme(axis.title = element_text(size=10))

